//
//  SelectCityViewController.m
//  ZPIOSEducation
//
//  Created by 董伟1 on 17/1/10.
//  Copyright © 2017年 mini1. All rights reserved.
//

#import "SelectCityViewController.h"
#import "SelectCityCell.h"

@interface SelectCityViewController () <UITableViewDelegate,UITableViewDataSource>
{
    NSString  *cityNameStr;
}

@property (weak, nonatomic) IBOutlet UITableView *mainTableView;

@end

@implementation SelectCityViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self.navigationController setNavigationBarHidden:NO animated:YES];
    
    self.title = @"选择城市";
    self.mainTableView.delegate = self;
    self.mainTableView.dataSource = self;
    self.mainTableView.tableFooterView = [[UIView  alloc] init];
    
    cityNameStr = @"";
    NSString  *cityName = [[ZPAuthStore  sharedZPAuthStore].userToDic  objectForKey:@"cityName"];
    if (cityName && ![cityName  isEqual:[NSNull  null]]) {
        cityNameStr = [NSString  stringWithFormat:@"%@",cityName];
    }
    
    if (!self.dataArray || self.dataArray.count == 0) {
        [self  requestCityList];
    }
}

#pragma mark - 网络相关
#pragma mark 城市列表
- (void)requestCityList
{
    NSDictionary  *paramDic = [NSDictionary  dictionaryWithObjectsAndKeys:[[ZPAuthStore  sharedZPAuthStore].userToDic  objectForKey:@"memberId"] ,@"teacherId", nil];
    [[ZPNetworking  sharedZPNetworking] POST:APIPPT@"teacher/getCityList.json" parameters:paramDic progress:^(NSProgress * _Nullable progress) {
        
    } success:^(id  _Nonnull responseObject) {
        NSLog(@"++++++++++%@",responseObject);
        if ([[responseObject  objectForKey:@"code"] integerValue] == 200) {
            if ([[responseObject  objectForKey:@"result"] isKindOfClass:[NSArray  class]]) {
                self.dataArray = [responseObject  objectForKey:@"result"];
                [self.mainTableView  reloadData];
            }
        }
        
    } failure:^(NSError * _Nonnull error) {
        
    }];
}

#pragma mark - UITableView Delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.dataArray  count];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 22.;
}

- (UIView  *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView  *tempView = [[UIView  alloc] initWithFrame:CGRectMake(0, 0, SCREENWIDTH, 22.)];
    tempView.backgroundColor = RGBACOLOR(238, 238, 238, 1.);
    
    UILabel  *tempLab = [[UILabel  alloc] initWithFrame:CGRectMake(12, 4, 200, 14)];
    tempLab.text = @"城市列表";
    tempLab.textColor = RGBACOLOR(153, 153, 153, 1.);
    tempLab.font = [UIFont  systemFontOfSize:12.];
    tempLab.textAlignment = NSTextAlignmentLeft;
    tempLab.backgroundColor = [UIColor  clearColor];
    [tempView  addSubview:tempLab];
    
    return tempView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 48.;
}

- (UITableViewCell  *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"SelectCityCell";
    SelectCityCell  *cell = (SelectCityCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"SelectCityCell" owner:self options:nil] lastObject];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    NSString  *tempStr = [NSString  stringWithFormat:@"%@",[[self.dataArray  objectAtIndex:indexPath.row] objectForKey:@"name"]];
    if ([tempStr  isEqualToString:cityNameStr]) {
        cell.titleLab.textColor = RGBACOLOR(255, 86, 86, 1.);
    }else {
        cell.titleLab.textColor = RGBACOLOR(51, 51, 51, 1.);
    }
    cell.titleLab.text = tempStr;
    
    return cell;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary  *tempDic =[self.dataArray  objectAtIndex:indexPath.row];
    
    NSString *homePath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/UserInfo.plist"];
    NSMutableDictionary *dataDic = [[NSDictionary dictionaryWithContentsOfFile:homePath] mutableCopy];
    [dataDic  setObject:[NSString  stringWithFormat:@"%@",[tempDic  objectForKey:@"name"]] forKey:@"cityName"];
    [dataDic  setObject:[NSString  stringWithFormat:@"%@",[tempDic  objectForKey:@"selectId"]] forKey:@"cityId"];
    [dataDic  writeToFile:homePath atomically:YES];
    [[ZPAuthStore  sharedZPAuthStore] resetUserInfoDictionary];
    [[ZPAuthStore  sharedZPAuthStore] setWebViewCookiesWithType:1];
    
    [[NSNotificationCenter  defaultCenter] postNotificationName:@"selectCityNotification" object:nil];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"refreshSearch" object:nil];

    [self.navigationController  popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
